## creates tables for appendix E:
## within party cleavages plot
## figure A7

rm(list=ls())

library(tidyverse)

# read in data

opinion <- readRDS('bootstrap_gaps_data/opinion_bootstrapped_noleaners_allcleavages.RDS')

issuetopics <- opinion$issuetopics
opinion <- opinion[names(opinion) != 'issuetopics']

calculate_cleavage <- function(df1, df2) {
  df <- abs(df1 - df2)
  df <- df %>% 
    as.data.frame() %>% 
    rownames_to_column('question')
  
  df <- df %>% 
    left_join(issuetopics %>% select(question, topic_6), by = 'question') %>% 
    bind_rows(df %>% 
                mutate(topic_6 = 'all')) %>% 
    drop_na() %>% 
    gather(key = sim, value = opinion, -question, -topic_6) %>% 
    group_by(sim, topic_6) %>% 
    summarize(pol = mean(opinion)) %>% 
    ungroup() %>% 
    group_by(topic_6) %>% 
    summarize(pol_mean = mean(pol),
              pol_95lo = quantile(sort(pol), 0.025),
              pol_95hi = quantile(sort(pol), 0.975))
  
  return(df)
}

cleave_list <- list()

for (p in c('all', 'rep', 'dem')) {
  print(p)
  
  # age
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_ageold_boot')]], opinion[[paste0(p, '_ageyoung_boot')]])
  tmp.df$cleavage <- 'age_oldyoung'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  # educ
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_educhs_boot')]], opinion[[paste0(p, '_educcoll_boot')]])
  tmp.df$cleavage <- 'educ_collhs'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_educnohs_boot')]], opinion[[paste0(p, '_educhs_boot')]])
  tmp.df$cleavage <- 'educ_hsnohs'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  # class
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_inctop_boot')]], opinion[[paste0(p, '_incbottom_boot')]])
  tmp.df$cleavage <- 'class_topbottom'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_inctop_boot')]], opinion[[paste0(p, '_incmiddle_boot')]])
  tmp.df$cleavage <- 'class_topmiddle'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  
  # race 
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_racewhite_boot')]], opinion[[paste0(p, '_raceblack_boot')]])
  tmp.df$cleavage <- 'race_whiteblack'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_racewhite_boot')]], opinion[[paste0(p, '_racehisp_boot')]])
  tmp.df$cleavage <- 'race_whitehisp'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_racehisp_boot')]], opinion[[paste0(p, '_raceblack_boot')]])
  tmp.df$cleavage <- 'race_blackhisp'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  
  # religion
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_religbornagain_boot')]], opinion[[paste0(p, '_relignotbornagain_boot')]])
  tmp.df$cleavage <- 'relig_bornagain'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_religchurchweekly_boot')]], opinion[[paste0(p, '_religchurchnever_boot')]])
  tmp.df$cleavage <- 'relig_church'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  
  # sex
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_sexfemale_boot')]], opinion[[paste0(p, '_sexmale_boot')]])
  tmp.df$cleavage <- 'sex'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
  # urbanrural
  
  tmp.df <- calculate_cleavage(opinion[[paste0(p, '_urban_boot')]], opinion[[paste0(p, '_rural_boot')]])
  tmp.df$cleavage <- 'urbanrural'
  tmp.df$party <- p
  cleave_list[[length(cleave_list)+1]] <- tmp.df
  
}

cleavages <- bind_rows(cleave_list)


cleavages_fig <- cleavages %>% 
  mutate(cleavage = case_when(cleavage == 'educ_hsnohs' ~ 'Education: H.S. vs. No H.S.',
                              cleavage == 'class_topmiddle' ~ 'Class: Top vs. Middle',
                              cleavage == 'race_blackhisp' ~ 'Race: Black vs. Latino',
                              cleavage == 'sex' ~ 'Sex',
                              cleavage == 'educ_collhs' ~ 'Education: College vs. H.S.',
                              cleavage == 'class_topbottom' ~ 'Class: Top vs. Bottom',
                              cleavage == 'age_oldyoung' ~ 'Age: 65+ vs. Under 30',
                              cleavage == 'race_whitehisp' ~ 'Race: White vs. Latino',
                              cleavage == 'urbanrural' ~ 'Urban vs. Rural',
                              cleavage == 'race_whiteblack' ~ 'Race: White vs. Black',
                              cleavage == 'relig_bornagain' ~ 'Religion: Born Again',
                              cleavage == 'relig_church' ~ 'Religion: Church Attendance'),
         party = case_when(party == 'all' ~ 'All Respondents',
                           party == 'dem' ~ 'Democrats',
                           party == 'rep' ~ 'Republicans'),
         topic_6 = case_when(topic_6 == 'all' ~ 'All Issues',
                             topic_6 == 'cultural' ~ 'Cultural',
                             topic_6 == 'economic' ~ 'Economic',
                             topic_6 == 'foreignpolicy' ~ 'Foreign policy',
                             topic_6 == 'immigration' ~ 'Immigration',
                             topic_6 == 'lawenforcement' ~ 'Law enforcement',
                             topic_6 == 'socialwelfare' ~ 'Social welfare'))

cleavages_fig$cleavage <- factor(cleavages_fig$cleavage, 
                                 levels = c(cleavages_fig[cleavages_fig$party == 'All Respondents'& 
                                                            cleavages_fig$topic_6 == 'All Issues'&
                                                            cleavages_fig$cleavage != 'Class: Top vs. Bottom',] %>% 
                                              arrange(-pol_mean) %>% pull(cleavage),
                                            'Class: Top vs. Bottom'))

ggplot(cleavages_fig,
       aes(x = pol_mean, y = cleavage)) +
  geom_point(aes(color = party, shape = cleavage == 'Class: Top vs. Bottom')) +
  geom_errorbar(aes(color = party, xmin = pol_95lo, xmax = pol_95hi), width = 0) +
  facet_grid(topic_6 ~ party) +
  labs(x = 'Opinion Gap', y = '') +
  theme_bw() +
  scale_color_manual(breaks = c('All Respondents', 'Republicans', 'Democrats'),
                     values = c('gray50', 'red', 'blue')) +
  scale_shape_manual(breaks = c(TRUE, FALSE),
                     values = c(16, 1)) +
  theme(legend.position = 'none')
ggsave('appendix_figures/compare_cleavages.png', width = 15, height = 18, scale = .75)
